<?php

	/********************************************************
	 * Requiered pages
	 ********************************************************/
/*
 * Databse main setting to connect
 */
require_once "db_settings.inc";
require_once "Flight.php";
require_once "User.php";

//Email of the administrator
define("EMAIL", "gustavo.a.lozana@student.uts.edu.au");

/**
 * 
 * The main class UtsAirs
 * Maneges all the worlds main function
 * @author gustavolozano
 *
 */
class UtsAirs
{
	/********************************************************
	 * Atributes
	 ********************************************************/
	
	/*
	 * Conection to the database
	 */
	var $dbc;
	
	/*
	 * Array list of all the flights booked by the user
	 */
	var $flights =  array();
	
	/*
	 * 
	 */
	var $user;
	
	
	/********************************************************
	 * Methods
	 ********************************************************/
	
	/**
	 * 
	 * Connects the database returning false or true depending if 
	 * it was succesfull or not
	 */
	function connectDataBase( )
	{	     
     	$this->dbc = new mysqli(DB_HOST,DB_QUERY_USER,DB_QUERY_PASS,WEA_DB);
     
	     if (mysqli_connect_errno()) 
	     {
	       	return FALSE;
	     }
	     
	     return TRUE;	
	}
	
	/**
	 * 
	 * Search flights on the database and returns an arraylist of
	 * all the flights found.
	 * @param String $pFromCity
	 * @param String $pToCity
	 */
	 function searchFlights($pFromCity,$pToCity) 
	 {
	 	$searchedFlights = array();
			
	 	if($this -> connectDataBase())
	 	{
	 		
	 		$sql = "SELECT from_city,to_city,price,route_no
	 				FROM flights 
	 				WHERE TRIM(UPPER(from_city)) = TRIM(UPPER('".$pFromCity."')) 
	 				OR   TRIM(UPPER(to_city)) = TRIM(UPPER('".$pToCity."'))";
	 		
	 		$result = mysqli_query($this->dbc,$sql);
	 	
		 	while($row = mysqli_fetch_array($result,MYSQLI_ASSOC))
			{
				$tempf = new Flight();
				$tempf->fromCity = $row["from_city"];
				$tempf->toCity = $row["to_city"];
				$tempf->price = $row["price"];
				$tempf->id = $row["route_no"];
				array_push($searchedFlights, $tempf);
			}
	 	}
	 	else
	 	{
	 		throw new Exception("Cant Connect to the database", "404");
	 	}
	 	
	 	mysqli_free_result($result);
	 	mysqli_close($this->dbc);
	 	return $searchedFlights;	
	 	
	 }
	 
	 /**
	  * 
	  * Enter description here ...
	  * @param unknown_type $flightNo
	  */
	 function getFlight($flightNo)
	 {
	 	$tempf = new Flight();
	 	
	 	if($this -> connectDataBase())
	 	{
	 		$sql = "SELECT from_city,to_city,price,route_no
	 				FROM flights
	 				WHERE route_no = ".$flightNo;
	 		
	 		$result = mysqli_query($this->dbc,$sql);
	
	 		if($row = mysqli_fetch_array($result,MYSQLI_ASSOC))
	 		{
				$tempf->fromCity = $row["from_city"];
				$tempf->toCity = $row["to_city"];
				$tempf->price = $row["price"];
				$tempf->id = $row["route_no"];
	 		}
	 	}
	 	mysqli_close($this->dbc);
	 	return $tempf;
	 	
	 }
	 
	 /**
	  * 
	  * Show the citys depending of the arriving or departing city
	  * @param String $toOrFrom departing or arriving
	  */
	 function getToCity($toOrFrom)
	 {
	 	$tempf = array();
	 	
	 	if($this -> connectDataBase())
	 	{
	 		$wich = "";
	 		
	 		if($toOrFrom == 'to')
		 		$wich = "to_city";
		 	else
	 			$wich = "from_city";
	 			
	 		$sql = "SELECT DISTINCT ".$wich.
		 				" FROM flights";
	 			
	 		$result = mysqli_query($this->dbc,$sql);
	
	 		while($row = mysqli_fetch_array($result,MYSQLI_ASSOC))
	 		{
				array_push($tempf, $row[$wich]);
	 		}
	 	}
	 	
	 	mysqli_close($this->dbc);
	 	return $tempf;
	 }
	 
	 
	 /**
	  * 
	  * Enter description here ...
	  * @param array $pFlights
	  */
	 function addFlights(Array $pFlights)
	 {
	 	array_merge($this->flights,$pFlights);	
	 }
	 
	 /**
	  * 
	  * Enter description here ...
	  * @param unknown_type $flight
	  */
	 function addFlight(Flight $flight)
	 {
	 	array_push($this->flights, $flight);
	 }
	 
	 function addSeat($routeNo, $seatNo)
	 {
	 	foreach ($this->flights as $f)
	 	{
	 		if($f->id == $routeNo)
	 		{
	 			$f->changeSeat($seatNo,"NORMAL");
	 		}
	 	}
	 }
	 
	 /**
	  * 
	  * Sends a mail to the user
	  * @param unknown_type $to
	  * @param unknown_type $subject
	  * @param unknown_type $message
	  */
	 function sendEmailToUser($to,$subject,$message)
	 {
	 	
	 	$mailheader = "MIME-Version: 1.0\r\n";
	 	$mailheader .= "Content-type:text/html; charset=ISO-8859-1\r\n";
	 	$mailheader .= "From: UTS AIRS <utsairsadmin@utsairs.com.au> \n";
	 	$mailheader .= "Reply- to: ". $to;
	 	
	 	mail($to, $subject, $message,$mailheader);
	 }
	 
	 /**
	  * 
	  * Send a mail o the administrator of the webpage
	  * @param unknown_type $to
	  * @param unknown_type $subject
	  * @param unknown_type $message
	  */
	 function sendEmailContactUS($subject, $message)
	 {
	 	
	 	mail(EMAIL, $subject, $message);
	 }
	 
	 /**
	  * 
	  * Enter description here ...
	  */
	 function getUser()
	 {
	 	return $this->user;
	 }
	 
	 /**
	  * 
	  * Enter description here ...
	  * @param unknown_type $pUser
	  */
	 function setUser($pUser)
	 {
	 	$this->user = $pUser;
	 } 
}

?>